home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1994 December / PSL Monthly Shareware CD-ROM (Public Software Library)(December 1994).bin / prgmming / dos / c1 / less.c < prev    next >
C/C++ Source or Header  |  1990-04-20  |  3KB  |  63 lines

  1. /*****************************************************\
  2. * LESS    Written by: Bruce Lowther [71520,2165]      *
  3. *                                                     *
  4. * type <filename> | less <scoll length>               *
  5. *                                                     *
  6. * Less is similar to More in MS-dos, but the scroll   *
  7. * length can be changed by entering the prefered      *
  8. * scroll length on the command line.                  *
  9. *                                                     *
  10. * Example:        c:\>type less.c | less 10           *
  11. *                                                     *
  12. * This program is an example of combinations of       *
  13. * the stdin stream, CON stream, and command line      *
  14. * parameters.  If you use this program, or find it    *
  15. * useful then yell "Thanks!" real loud, or drop me    *
  16. * mail.  I would also like some constructive crit.,   *
  17. * if you have any.                                    *
  18. \*****************************************************/
  19.  
  20. #include <stdio.h>    /* for fopen(), getchar() and getc() */
  21. #include <stdlib.h>    /* for atoi() */
  22. #define SMALL 15        /* value of scroll if none is specified */
  23.  
  24. main (int argc,char *argv[])                /* gets the command line parameters */
  25. {
  26.     FILE *kbd;                                    /* file used when CON is opened    */
  27.     int counter = 1,                            /* the line number counter            */
  28.          letter,                                    /* character from stdin                */
  29.          len;                                        /* scroll length                        */
  30.  
  31.  
  32.     if (argc == 1)                                /* if no command line parameter,
  33.                                                         set to standard scroll        */
  34.         len = SMALL;
  35.     else                                            /* else, take command line,
  36.                                                         convert it to integer and use
  37.                                                         it                            */
  38.         len = atoi(argv[1]);
  39.  
  40.     while ((letter = getchar()) != EOF) /* loop until EOF on stdin.      */
  41.     {
  42.         if (counter >= len)                    /* if counter has reached len,
  43.                                                         then stop, and get input from
  44.                                                         the CON (keyboard)                */
  45.         {
  46.             printf("\n%d[LESS]", len);
  47.             kbd = fopen("CON","r");            /* open the CON, for read only
  48.                                                         note that stdin has not been
  49.                                                         closed.                                */
  50.             getc(kbd);                            /* get a character from the
  51.                                                         keyboard                                */
  52.             fclose (kbd);                        /* close keyboard so input can
  53.                                                         come from stdin once again.    */
  54.             counter = 1;
  55.         }
  56.         if (letter == '\n') counter++;    /* increment counter at each
  57.                                                         newline character '\n'            */
  58.         putchar(letter);                        /* put the next character onto
  59.                                                         stdout.                                */
  60.     }
  61. }
  62.  
  63.